package com.alog.yzd.bookmall.dao.catagory;

import com.alog.yzd.bookmall.domain.catagory.Catagory;
import com.alog.yzd.bookmall.utils.jdbc.TxQueryRunner;
import com.alog.yzd.commons.CommonUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 分类的持久层
 */
public class CatagoryDao {
    private QueryRunner qr = new TxQueryRunner();

    /**
     * 删除分类
     */
    public void delete(String cid) throws SQLException {
        String sql = "delete from t_category where cid = ?";
        qr.update(sql, new Object[]{cid});
    }
    /**
     * 查询父分类下子分类的个数
     */
    public int findChildCountByParent(String pid) throws SQLException {
        String sql = "select count(*) from t_category where pid = ?";
        Number number = qr.query(sql, new ScalarHandler<>(), new Object[]{pid});
        return number == null ? 0 : number.intValue();
    }

    /**
     * 修改分类
     */
    public void edit(Catagory catagory) throws SQLException {
        String sql = "update t_category set cname = ?, pid = ?, `desc` = ? where cid = ?";
        qr.update(sql, new Object[]{catagory.getCname(), catagory.getParent() == null ? null : catagory.getParent().getCid(),
                catagory.getDesc(), catagory.getCid()});
    }

    /**
     * 加载Catagory
     */
    public Catagory load(String cid) throws SQLException {
        String sql = "select * from t_category where cid = ?";
        Map<String, Object> map = qr.query(sql, new MapHandler(), new Object[]{cid});
        Catagory catagory = toCatagory(map);
        return catagory;
    }

    /**
     * 查询一级分类
     */
    public List<Catagory> findParents() throws SQLException {
        String sql = "select * from t_category where pid is null order by orderBy";
        List<Map<String, Object>> mapList = qr.query(sql, new MapListHandler());
        List<Catagory> parents = toCatagoryList(mapList);
        return parents;
    }

    /**
     *  添加分类
     */
    public void add(Catagory catagory) throws SQLException {
        String sql = "insert into t_category (cid, cname, pid, `desc`) values (?, ?, ?, ?)";
        String pid = null;
        if(catagory.getParent() != null){
            pid = catagory.getParent().getCid();
        }
        Object[] params = new Object[]{catagory.getCid(), catagory.getCname(), pid, catagory.getDesc()};
        qr.insert(sql, new ScalarHandler<>(), params);
    }

    /**
     * 根据父分类查询对应的所有的子分类
     * @param pid
     * @return
     * @throws SQLException
     */
    public List<Catagory> findByParent(String pid) throws SQLException {
        String sql = "select * from t_category where pid = ? order by orderBy";
        Object[] params = new Object[]{pid};
        List<Map<String, Object>> mapList = qr.query(sql, new MapListHandler(), params);
        List<Catagory> childs = toCatagoryList(mapList);
        return childs;
    }

    /**
     * 查询所有父分类, 父分类中包含有子分类
     * @return
     * @throws SQLException
     */
    public List<Catagory> findAll() throws SQLException {
        String sql = "select * from t_category where pid is null order by orderBy";
        List<Map<String, Object>> mapList = qr.query(sql, new MapListHandler());
        List<Catagory> parents = toCatagoryList(mapList);
        for(Catagory parent : parents){
            List<Catagory> childs = findByParent(parent.getCid());
            parent.setChilds(childs);
        }
        return parents;
    }

    /**
     * 将一个Map转成Catagory对象
     * @param map
     * @return
     */
    private Catagory toCatagory(Map<String, Object> map){
        Catagory catagory = CommonUtils.toBean(map, Catagory.class);
        String pid = (String) map.get("pid");
        if(pid != null){
            Catagory parent = new Catagory();
            parent.setCid(pid);
            catagory.setParent(parent);
        }
        return catagory;
    }

    /**
     * 将一个MapList转成一个CatagoryList
     * @param mapList
     * @return
     */
    private List<Catagory> toCatagoryList(List<Map<String, Object>> mapList){
        List<Catagory> catagories = new ArrayList<>();
        for(Map<String , Object> map : mapList){
            Catagory catagory = toCatagory(map);
            catagories.add(catagory);
        }
        return catagories;
    }
}
